/**
 * 
 */
package com.toncen.samepms.common;

import java.util.ArrayList;
import java.util.List;

import com.toncen.samepms.basic.dao.UserDao;
import com.toncen.samepms.basic.domain.Organization;
import com.toncen.samepms.basic.domain.User;
import com.toncen.samepms.system.domain.Role;

/**
 * 描述：默认可操作数据权限提供者
 * 
 * @author tch
 */
public class DefaultAccessPermissionProvider extends AbstractAccessPermissionProvider {
	/**
	 * 
	 */
	public DefaultAccessPermissionProvider() {
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		AbstractAccessPermissionProvider accessPermissionProvider = new DefaultAccessPermissionProvider();
		Object context = null;
		User user = null;
		user = new UserDao<User>().load(1331626056324L);
		System.out.println("用户权限:" + accessPermissionProvider.getAccessUserPermissions(context, user));
		System.out.println("组织权限:" + accessPermissionProvider.getAccessOrganizationPermissions(context, user));
		System.out.println("角色权限:" + accessPermissionProvider.getAccessRolePermissions(context, user));
		user = new UserDao<User>().load(1331693256859L);
		System.out.println("用户权限:" + accessPermissionProvider.getAccessUserPermissions(context, user));
		System.out.println("组织权限:" + accessPermissionProvider.getAccessOrganizationPermissions(context, user));
		System.out.println("角色权限:" + accessPermissionProvider.getAccessRolePermissions(context, user));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAccessPermissionProvider#getAccessPermissions(java.lang.Object,
	 *      com.toncen.samepms.basic.domain.User, java.lang.String)
	 */
	@Override
	public List<AbstractDomain> getAccessPermissions(Object context, User user, String type) throws Exception {
		List<AbstractDomain> accessPermissions = new ArrayList<AbstractDomain>();
		if (User.class.getSimpleName().equalsIgnoreCase(type)) {// 按照用户过滤数据
			accessPermissions.addAll(getAccessUserPermissions(context, user));
		} else if (("Self" + User.class.getSimpleName()).equalsIgnoreCase(type)) {// 按照当前用户过滤数据
			accessPermissions.add(user);
		} else if (Organization.class.getSimpleName().equalsIgnoreCase(type)) {// 按照组织过滤数据
			accessPermissions.addAll(getAccessOrganizationPermissions(context, user));
		} else if (("Self" + Organization.class.getSimpleName()).equalsIgnoreCase(type)) {// 按照当前组织过滤数据
			accessPermissions.add(user.getOrganization());
		} else if (Role.class.getSimpleName().equalsIgnoreCase(type)) {// 按照角色过滤数据
			accessPermissions.addAll(getAccessRolePermissions(context, user));
		} else if (("Self" + Role.class.getSimpleName()).equalsIgnoreCase(type)) {// 按照当前角色过滤数据
			accessPermissions.addAll(user.getRoles());
		} else {
			throw new Exception("未支持的可操作数据类型【" + type + "】，请与系统管理员联系！");
		}
		return accessPermissions;
	}

}
